package com.example.crawler.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;


public class DateUtils{

    public final static String DEFAULT_PATTERN = "yyyy-MM-dd";

    /**
     * 获取上个季度的时间范围
     * @return
     */
    public static Date getLastQuarter() {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);
        return endCalendar.getTime();
    }

    private static void setMaxTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }

    public static String getLastTenYear(){
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_PATTERN);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getLastQuarter());
        StringBuilder dateStr = new StringBuilder();
        for(int i = 0; i < 11; i++){
            Date y = calendar.getTime();
            String year = format.format(y);
            dateStr.append("'" + year + "',");
            calendar.add(Calendar.YEAR, -1);
        }

        return dateStr.substring(0, dateStr.length()-1);
    }
    public static String getTenYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getLastQuarter());
        StringBuilder dateStr = new StringBuilder();
        for(int i = 0; i < 11; i++){
            Date y = calendar.getTime();
            String year = format.format(y);
            dateStr.append("'" + year + "',");
            calendar.add(Calendar.YEAR, -1);
        }

        return dateStr.substring(0, dateStr.length()-1);
    }


    /**
     * 获取当前日期的字符串，格式为：yyyyMMdd
     */
    public static String getDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return "'" + sdf.format(new Date()) + "'";
    }

    /**
     * 获取当前日期的字符串，格式为：yyyyMMdd
     */
    public static String getYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    /**
     * 获取 N 天之后的日期的字符串，格式为：yyyyMMdd
     * @param date 传入日期
     * @param offset 日期偏移天数
     * @return
     */
    public static String getOffsetDate(Date date, Integer offset){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, offset);
        Date resultDate = calendar.getTime();
        return "'" + sdf.format(resultDate) + "'";
    }

    /**
     * 获取 N 天之后的日期的字符串，格式为：yyyyMMdd
     * @param date 传入日期
     * @param offset 日期偏移年
     * @return
     */
    public static String getOffsetYear(Date date, Integer offset){
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, offset);
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    /**
     * 格式化 map 中的日期，格式化后效果：2022-09-09
     * @param keyName 键
     * @param map 格式化的 map
     * @return
     */
    public static String getValue(String keyName, HashMap map) {
        if(map.get(keyName) == null ){
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(new Date());
        } else {
            return String.valueOf(map.get(keyName)).substring(0, 4) + "-"
                    + String.valueOf(map.get(keyName)).substring(4, 6) + "-"
                    + String.valueOf(map.get(keyName)).substring(6, 8);
        }

    }

}
